package cyx.example.ddd.domain.order.model.valueobject;

import cyx.example.ddd.domain.shared.ValueObject;
import lombok.Value;

import java.math.BigDecimal;
import java.util.Currency;

/**
 * 金额值对象
 */
@Value
public class Money implements ValueObject {
    BigDecimal amount;
    Currency currency;

    public static Money ZERO(Currency currency) {
        return new Money(BigDecimal.ZERO, currency);
    }

    public Money add(Money money) {
        if (!this.currency.equals(money.currency)) {
            throw new IllegalArgumentException("Cannot add money with different currencies");
        }
        return new Money(this.amount.add(money.amount), this.currency);
    }

    public Money multiply(int multiplier) {
        return new Money(this.amount.multiply(BigDecimal.valueOf(multiplier)), this.currency);
    }

    public boolean isGreaterThanOrEqual(Money other) {
        if (!this.currency.equals(other.currency)) {
            throw new IllegalArgumentException("Cannot compare money with different currencies");
        }
        return this.amount.compareTo(other.amount) >= 0;
    }
}